//
//  ViewController.m
//  RunloopTableViewDemo
//
//  Created by Jack on 2021/6/30.
//

#import "ViewController.h"
#import "JKLogManager.h"
#import "ImageCell.h"

#define JK_Main_Height [UIScreen mainScreen].bounds.size.height
#define JK_Main_Width  [UIScreen mainScreen].bounds.size.width

static NSString *const cId = @"imageCell";

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>{
    BOOL _isScrolling;
}
@property(nonatomic, strong)JKLogView *logView;
@property(nonatomic, strong)UITableView *imageTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isScrolling = NO;
    [self.view addSubview:self.logView];
    [self.view addSubview:self.imageTableView];
}

- (JKLogView *)logView{
    if (!_logView) {
        _logView = [JKLogManager sharedManager].logView;
        _logView.frame = CGRectMake(JK_Main_Width/2, 0, JK_Main_Width/2, JK_Main_Height);
    }
    return _logView;
}

- (UITableView *)imageTableView{
    if (!_imageTableView) {
        _imageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, JK_Main_Width/2, JK_Main_Height) style:(UITableViewStylePlain)];
        _imageTableView.delegate = self;
        _imageTableView.dataSource = self;
    }
    return _imageTableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:cId];
    if (!cell) {
        cell = [[ImageCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cId];
    }
    [cell requestLayoutWithTitle:[NSString stringWithFormat:@"%@",@(indexPath.row)] isScrolling:_isScrolling];
    return cell;
}

/**
 Decelerate减速
 */

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    /**
     拉动scrollview，开始减速的时候回调
     */
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    /**
     拉动scrollview，已经减速的时候回调
     */
    _isScrolling = NO;
    if (scrollView == self.imageTableView) {
        NSArray *indexArray = [self.imageTableView indexPathsForVisibleRows];
        for (NSIndexPath *indexPath in indexArray) {
            ImageCell *cell = [self.imageTableView cellForRowAtIndexPath:indexPath];
            [cell requestLayoutWithTitle:[NSString stringWithFormat:@"%@",@(indexPath.row)] isScrolling:_isScrolling];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    /**
     拉动的时候就会进入的回调
     */
    _isScrolling = YES;
}


@end
